// Copyright (C) 2010 Razvan Constantin <razvan.constantin@rconst.net>
//
// This file is part of FileBow.
//
// FileBow is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// FileBow is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with FileBow.  If not, see <http://www.gnu.org/licenses/>.
//
// Test cases for Navigator.

#include "logic/navigator.h"

#include <algorithm>
#include <memory>
#include <gtest/gtest.h>

#include "common/strings.h"
#include "testing/test_drive.h"

using namespace std;

namespace filebow {
namespace testing {

class NavigatorTest : public ::testing::Test {
  protected: static void SetUpTestCase() {
    mTestDrive.reset(new TestDrive);
  }
  protected: static void TearDownTestCase() {
    mTestDrive.reset();
  }

  protected: virtual void SetUp() {
    mNavigator.reset(new Navigator);
  }
  protected: virtual void TearDown() {
    mNavigator.reset();
  }

  protected: static auto_ptr<TestDrive> mTestDrive;
  protected: auto_ptr<Navigator> mNavigator;
  protected: vector<string> mTop;
};
auto_ptr<TestDrive> NavigatorTest::mTestDrive;

TEST_F(NavigatorTest, FunctionalTest) {
  // Initialize.
  mNavigator->AddGlobalPath(mTestDrive->path() + "/global");
  mNavigator->AddProjectPath(mTestDrive->path() + "/project");
  // Simulate opened and active files.
  mNavigator->RecordPreviouslyOpened(mTestDrive->path() + "/project/file11");
  mNavigator->SetActiveFile(mTestDrive->path() + "/project/dir12/file123.h");
  mNavigator->WaitScan();
  // Build top for this state.
  mNavigator->BuildTop("", 10, &mTop);
  ASSERT_EQ(10, mTop.size()) << Join(mTop, ", ");
  EXPECT_TRUE(mTop.end() == find(mTop.begin(), mTop.end(), "dir12/file123.h"));
  EXPECT_EQ("dir12/file123.cpp", mTop[0]);
  EXPECT_EQ("dir12/file123_test.cpp", mTop[1]);
  EXPECT_EQ("file11", mTop[2]);
  mTop.clear();
  // Simulate close and change active file.
  mNavigator->RecordClose(mTestDrive->path() + "/project/file11");
  mNavigator->SetActiveFile(mTestDrive->path() + "/project/dir12/file123.cpp");
  // Build top for this state.
  mNavigator->BuildTop("", 10, &mTop);
  ASSERT_EQ(10, mTop.size());
  EXPECT_TRUE(mTop.end() == find(mTop.begin(), mTop.end(), "dir12/file123.cpp"));
  EXPECT_EQ("dir12/file123.h", mTop[0]);
  EXPECT_EQ("dir12/file123_test.cpp", mTop[1]);
  mTop.clear();
}

} // namespace testing
} // namespace filebow
